package com.meyoung.day2;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

import static java.lang.Thread.*;

public class ActionsTest2 {
    WebDriver driver;
    @BeforeMethod
    public void openchromeTest01(){
        //设置chromedriver路径
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\cll520\\IdeaProjects\\selenium11\\drivers\\chromedriver.exe");
        //实例化Chromedriver
        driver = new ChromeDriver();
        //全局等待
        //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }
    /*
    打开百度页面
    鼠标右击百度一下

     */
    @Test
    public void rightChilkTest() throws InterruptedException {
        driver.get("https://www.baidu.com");
        //定位元素
        WebElement buttonBaidu = driver.findElement(By.id("su"));
        //实例化Actions
        Actions actions = new Actions(driver);
        //右击
        actions.contextClick(buttonBaidu).perform();
        sleep(3000);
    }
    /*
打开百度页面
鼠标双击百度一下

 */
    @Test
    public void doubleChilkTest() throws InterruptedException {
        driver.get("https://www.baidu.com");
        WebElement doublechilk = driver.findElement(By.id("su"));
        Actions actions = new Actions(driver);
        //双击
        actions.doubleClick(doublechilk).perform();
        sleep(3000);
    }
    /*
打开测试页面
鼠标移动到action按钮
显示Hello World！

*/
    @Test
    public void move() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        WebElement move = driver.findElement(By.xpath("//*[@id=\"action\"]/input"));
        Actions actions = new Actions(driver);
        //移动显示
        actions.moveToElement(move).perform();
        sleep(3000);
        //验证文本
        String hello = driver.findElement(By.xpath(".//*[text()='Hello World!']")).getText();
        Assert.assertEquals(hello,"Hello World!");
    }
        /*
打开测试页面
鼠标拖拽到某一点


*/
    @Test
    public void testDrop1() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/dragAndDrop.html");
        WebElement drop = driver.findElement(By.xpath("//*[@id=\"drag\"]"));
        Actions actions = new Actions(driver);
        actions.dragAndDropBy(drop,500,500).perform();
        Thread.sleep(3000);

    }
            /*
打开测试页面
鼠标拖拽到另一个元素上面


*/
     @Test
     public void testDrop2() throws InterruptedException {
         driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/dragAndDrop.html");
         WebElement el1 = driver.findElement(By.xpath("//*[@id=\"drag\"]"));
         WebElement el2 = driver.findElement(By.xpath("/html/body/h1"));
         //实例化actions
         Actions actions = new Actions(driver);
         //点住（按住）
         actions.clickAndHold(el1)
                 //移动到元素
                 .moveToElement(el2)
                 //释放第一个元素，松开按键
                 .release()
                 .perform();
         Thread.sleep(3000);
     }
    /*
打开测试页面
下拉框多选
选择前3个


*/
    @Test
    public void moreSelectTest() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
        List<WebElement> list = driver.findElements(By.xpath("//*[@id=\"selectWithMultipleEqualsMultiple\"]/option"));
        Thread.sleep(5000);
        Actions actions = new Actions(driver);
        //按住shift键
        actions.keyDown(Keys.SHIFT)
                //选择第一个
                .click(list.get(0))
                //选择第三个
                .click(list.get(2))
                //松开shift键
                .keyUp(Keys.SHIFT)
                .perform();
        Thread.sleep(5000);
    }
    /*
打开测试页面
下拉框多选
选择第1个
和第3个
*/
    @Test
    public void moreSelectTest1() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        WebElement element = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
        List<WebElement> list = driver.findElements(By.xpath("//*[@id=\"selectWithMultipleEqualsMultiple\"]/option"));
        Thread.sleep(5000);
        Actions actions = new Actions(driver);
        //按住Ctrl键
        actions.keyDown(Keys.CONTROL)
                //选择第三个
                .click(list.get(2))
                //松开Ctrl键
                .keyUp(Keys.CONTROL)
                .perform();
        Thread.sleep(5000);
    }

/*
打开百度页面
进行保存操作
 */
    @Test
    public void savaHtml() throws AWTException, InterruptedException {
        driver.get("http://www.baidu.com");
        //实例化Robot
        Robot robot = new Robot();
        //按住Ctrl键
        robot.keyPress(KeyEvent.VK_CONTROL);
        //得到S键的ASCII值
        int keyS = (int)new Character('S');
        //输出S键的ASCII值
        System.out.println(keyS);
        //按住s键
        robot.keyPress(keyS);
        Thread.sleep(2000);
        //按住enter键
        robot.keyPress(KeyEvent.VK_ENTER);
        //松开s键
        robot.keyRelease(keyS);
        //松开ctrl键
        robot.keyRelease(KeyEvent.VK_CONTROL);
        //松开enter键
        robot.keyRelease(KeyEvent.VK_ENTER);
    }
    /*
    打开测试页面
    上传文件
     */
    @Test
    public void uploadTest() throws InterruptedException {
        driver.get("file:///C:/Users/cll520/Desktop/%E5%AD%A6%E4%B9%A0%E8%B5%84%E6%96%99/index.html");
        driver.findElement(By.id("load")).sendKeys("C:\\Users\\cll520\\Pictures\\2.png");
        Thread.sleep(3000);
    }

    @AfterMethod
    public void closedBrowser(){
        driver.quit();
    }
}
